/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.view.charts;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Stroke;
import org.jfree.chart.annotations.XYDrawableAnnotation;
import org.jfree.chart.annotations.XYLineAnnotation;
import org.jfree.chart.annotations.XYPointerAnnotation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.ui.TextAnchor;
import systole.domain.analysis.results.Parameter;
import systole.domain.analysis.results.ParameterCoord;
import systole.domain.analysis.results.ParametersKind;

/**
 *
 * @author user
 */
public class XYParameter {

    private XYPointerAnnotation startLabel;
    private XYDrawableAnnotation startPoint;
    private XYPointerAnnotation endLabel;
    private XYDrawableAnnotation endPoint;
    private XYLineAnnotation joinPath;
    private XYLineAnnotation aidPath = null;
    private Color fillPaint;
    private final int RADIUS = 5;
    private final Stroke solidStroke = new BasicStroke(1.5f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER);
    private final Stroke dashedStroke = new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER, 15.0f, new float[]{9}, 0.0f);
    private Parameter paramModel;

    public XYParameter() {
    }

    public XYParameter(Parameter p, Color paint) {
        this.fillPaint = paint;
        this.paramModel = p;
        this.createGraphicParam();
    }

    public void createGraphicParam() {
        this.setStartLabel(this.createLabelAnnotation("P1", this.paramModel.getInitPos(), 17.0 * Math.PI / 16.0));
        this.startPoint = this.createPointAnnotation(this.paramModel.getInitPos());

        this.setEndLabel(this.createLabelAnnotation("P2", this.paramModel.getEndPos(), Math.PI / 4.0));
        this.endPoint = this.createPointAnnotation(this.paramModel.getEndPos());
        this.createLines();
    }

    private XYPointerAnnotation createLabelAnnotation(String label, ParameterCoord p, double angle) {
        XYPointerAnnotation pointer = new XYPointerAnnotation(label, p.getxCoord().doubleValue(),
                p.getyCoord().doubleValue(), angle);
        pointer.setBaseRadius(35.0);
        pointer.setTipRadius(15.0);
        pointer.setArrowPaint(this.fillPaint);
        pointer.setFont(new Font("SansSerif", Font.BOLD, 12));
        pointer.setPaint(this.fillPaint);
        pointer.setTextAnchor(TextAnchor.HALF_ASCENT_RIGHT);
        return pointer;
    }

    private XYDrawableAnnotation createPointAnnotation(ParameterCoord p) {
        CircleDrawer cd = new CircleDrawer(this.fillPaint, new BasicStroke(1.0f), this.fillPaint);
        return new XYDrawableAnnotation(p.getxCoord().doubleValue(), p.getyCoord().doubleValue(), this.RADIUS, this.RADIUS, cd);
    }

    private void createLines() {

        if (this.paramModel.getDescription().equals(ParametersKind.AOD) || this.paramModel.getDescription().equals(ParametersKind.T)) {
            ParameterCoord start = this.paramModel.getInitPos();
            ParameterCoord end = this.paramModel.getEndPos();
            ParameterCoord aid = new ParameterCoord(end.getxCoord(), start.getyCoord());

            if (this.paramModel.getDescription().equals(ParametersKind.AOD)) {
                this.joinPath = createLineAnnotation(start, aid, this.dashedStroke);
                this.aidPath = createLineAnnotation(aid, end, this.solidStroke);
            } else {
                this.joinPath = createLineAnnotation(start, aid, this.solidStroke);
                this.aidPath = createLineAnnotation(aid, end, this.dashedStroke);
            }
        } else {
            this.joinPath = this.createLineAnnotation(this.paramModel.getInitPos(), this.paramModel.getEndPos(), this.solidStroke);
        }

    }

    private XYLineAnnotation createLineAnnotation(ParameterCoord start, ParameterCoord end, Stroke typeline) {
        return new XYLineAnnotation(start.getxCoord().doubleValue(), start.getyCoord().doubleValue(), end.getxCoord().doubleValue(), end.getyCoord().doubleValue(), typeline, this.fillPaint);
    }

    /**
     * @return the startPoint
     */
    public XYDrawableAnnotation getStartPoint() {
        return startPoint;
    }

    /**
     * @param startPoint the startPoint to set
     */
    public void setStartPoint(XYDrawableAnnotation startPoint) {
        this.startPoint = startPoint;
    }

    /**
     * @return the endPoint
     */
    public XYDrawableAnnotation getEndPoint() {
        return endPoint;
    }

    /**
     * @param endPoint the endPoint to set
     */
    public void setEndPoint(XYDrawableAnnotation endPoint) {
        this.endPoint = endPoint;
    }

    /**
     * @return the joinPath
     */
    public XYLineAnnotation getJoinPath() {
        return joinPath;
    }

    /**
     * @param joinPath the joinPath to set
     */
    public void setJoinPath(XYLineAnnotation joinPath) {
        this.joinPath = joinPath;
    }

    /**
     * @return the fillPaint
     */
    public Color getFillPaint() {
        return fillPaint;
    }

    /**
     * @param fillPaint the fillPaint to set
     */
    public void setFillPaint(Color fillPaint) {
        this.fillPaint = fillPaint;
    }

    /**
     * @return the aidPath
     */
    public XYLineAnnotation getAidPath() {
        return aidPath;
    }

    /**
     * @param aidPath the aidPath to set
     */
    public void setAidPath(XYLineAnnotation aidPath) {
        this.aidPath = aidPath;
    }

    public boolean existsAidPath() {
        if (this.aidPath != null) {
            return true;
        }
        return false;
    }

    public void paintInChart(XYPlot plot) {
        plot.addAnnotation(this.getStartLabel());
        plot.addAnnotation(this.getStartPoint());
        plot.addAnnotation(this.getEndLabel());
        plot.addAnnotation(this.getEndPoint());
        plot.addAnnotation(this.getJoinPath());
        if (this.existsAidPath()) {
            plot.addAnnotation(this.getAidPath());
        }
    }

    /**
     * @return the startLabel
     */
    public XYPointerAnnotation getStartLabel() {
        return startLabel;
    }

    /**
     * @param startLabel the startLabel to set
     */
    public void setStartLabel(XYPointerAnnotation startLabel) {
        this.startLabel = startLabel;
    }

    /**
     * @return the endLabel
     */
    public XYPointerAnnotation getEndLabel() {
        return endLabel;
    }

    /**
     * @param endLabel the endLabel to set
     */
    public void setEndLabel(XYPointerAnnotation endLabel) {
        this.endLabel = endLabel;
    }
}
